/*   inupt 单选  */
;(function ( $, window, document, undefined ) {
    // 声明默认属性对象
    var pluginName = "topNetRadio";
    var defaults = {
        inputName:"",
        rendersData:[],
        checkedIds:'',
        radioClick:function ($el) {

        }
    };

    function radioPlugin(element,settings) {
        this.element = $(element);
        this.settings = $.extend({},defaults,settings);
        this.init();
    }
    radioPlugin.prototype = {
        init:function () {
            var add_str = '',_this = this;
            this.backParaName = '';
            this.backParaId = '';
            if(this.settings.rendersData.length>0){
                //渲染节点

                for (var i = 0;i<this.settings.rendersData.length;i++) {
                		var pDisabled = this.settings.rendersData[i].disabled ? 'disabled' : ''
                    add_str += '<div class="radio-div ' + pDisabled + '"  data-disabled = ' + pDisabled + '>' +
                        '<input class="input-radio"  type="radio" name="' + this.settings.inputName +'" value="'+ this.settings.rendersData[i].key +'"/>' +
                        '<span class="label-radio-icon"></span>' +
                        '<span class="radio-text">' + this.settings.rendersData[i].value + '</span>' +
                        '</div>';
                }
                this.element.html(add_str);
            }
            //渲染回显数据
            if(this.settings.checkedIds){
                $.each(_this.element.find('.radio-div'),function (index) {
                    if(_this.settings.checkedIds == _this.element.find('.radio-div').eq(index).children('.input-radio').val()){
                        _this.element.find('.radio-div').eq(index).addClass('checked').children('input').prop('checked',true);
                    }
                })
            }
            //判断是否只读
            if(this.element.hasClass('readonly')){
                this.element.children(".radio-div").css({'cursor':'not-allowed'});
                // this.element.css({'cursor':'not-allowed'});
                return;
            }
            //调用单选按钮点击事件
            var _this=this;
            this.element.children(".radio-div").off('click').on('click',function () {
								if($(this).attr('data-disabled')){
									return
								}
                $(this).addClass('checked').children('input').prop('checked',true);
                $(this).siblings().removeClass('checked').children('input').removeProp('checked');
                _this.settings.radioClick($(this).children('input'))
            });
        },
        //单选按钮点击事件
        radioClick:function() {
					$(this).addClass('checked').children('input').prop('checked',true);
					$(this).siblings().removeClass('checked').children('input').removeProp('checked');
        },
        //返回选中信息
        backName:function () {
            var _this = this;
            $.each(this.element.find('.radio-div'),function (index) {
                if(_this.element.find('.radio-div').eq(index).hasClass('checked')){
                    _this.backParaName = _this.element.find('.radio-div').eq(index).children('.radio-text').html();
                }
            });
            return this.backParaName;
        },
        backId:function () {
            var _this = this;
            $.each(this.element.find('.radio-div'),function (index) {
                if(_this.element.find('.radio-div').eq(index).hasClass('checked')){
                    _this.backParaId = _this.element.find('.radio-div').eq(index).children('input').val();
                }
            });
            return this.backParaId;
        },
        //重置
        resetRadio:function () {
            var _this = this;
            //所有单选按钮初始到未选中状态
            this.element.find('.radio-div').removeClass('checked').children('input').removeProp('checked');
            this.backParaName = '';
            this.backParaId = '';
            //需要回显的添加相应状态
            if(this.settings.checkedIds){
                $.each(_this.element.find('.radio-div'),function (index) {
                    if(_this.settings.checkedIds == _this.element.find('.radio-div').eq(index).children('.input-radio').val()){
                        _this.element.find('.radio-div').eq(index).addClass('checked').children('input').prop('checked',true);
                    }
                })
            }
        },
        //清空实例
        emptyInstance:function () {
            this.element.find('.radio-div').removeClass('checked').children('input').removeProp('checked');
            this.element.data('plugin_' + pluginName,null);
        }

    };
    // 对构造函数的一个轻量级封装，
    // 防止产生多个实例
    $.fn[pluginName] = function ( settings ) {
        var args = arguments;
        if (settings === undefined || typeof settings === 'object') {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new radioPlugin(this, settings));
                }
            });
        }else if (typeof settings === 'string' && settings[0] !== '_' && settings !== 'init') {
            var returns;
            this.each(function () {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof radioPlugin && typeof instance[settings] === 'function') {
                    returns = instance[settings].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
            return returns !== undefined ? returns : this;
        }
    };
})( jQuery, window, document );





/*
* 构造函数
* 多选
* */
;(function ( $, window, document, undefined ) {
    // 声明默认属性对象
    var pluginName = "topNetCheckbox";
    var defaults = {
        rendersData:[],      //  动态渲染：必填   静态：不填     动态dom节点渲染数据，数据结构如上
        inputName:"",        //  input 标签 name属性值
        labelclick:"",       //  全选框类名，如果需要全选按钮  则传递 全选按钮唯一性标识
        checkedIds:[],       //  选填，回显/默认选中复选按钮 对应id数组值
        checkedNum:0,
        backParaNameArr : [],
        backParaIdArr : [],
    };

    function CheckboxPlugin(element,settings) {
        this.element = $(element);
        this.settings = $.extend({},defaults,settings);
        this.init();
    }

    CheckboxPlugin.prototype = {
        //初始化
        init: function () {
            var add_div = "";
            if(this.settings.rendersData.length>0) {
                //dom结构渲染
                for (var i = 0; i < this.settings.rendersData.length; i++) {
                    add_div += '<div class="checkbox-div">' +
                        '<input class="input-checkbox" type="checkbox" name="' + this.settings.inputName + '" value="' + this.settings.rendersData[i].id + '"/>' +
                        '<span class="label-checkbox-icon"></span>' +
                        '<span class="checkbox-text">' + this.settings.rendersData[i].name + '</span>' +
                        '</div>';
                }
                this.element.html(add_div);
                /*	显示全选按钮	*/
                if (this.settings.labelclick != "") {
                    var cleck_span = '<div class="checkbox-state-all"><span class="checkbox-all label-checkbox-icon"></span><span>全选</span></div>';
                    $('div[labelclick=' + this.settings.labelclick + ']').html(cleck_span);
                }
            }

            //渲染回显数据
            if(this.settings.checkedIds.length>0){
                for(var i=0;i< this.settings.checkedIds.length;i++){
                    for(var j=0;j< this.element.find('.checkbox-div').length;j++) {
                        if (this.element.find('.checkbox-div').eq(j).children('input').val() == this.settings.checkedIds[i]) {
                            this.element.find('.checkbox-div').eq(j).addClass('checked').children('input').prop('checked', true);
                            this.settings.checkedNum++;
                        }
                    }
                }
            }
            //是否只读
            if(this.element.hasClass('readonly')){
                this.element.children('.checkbox-div').css({'cursor':'not-allowed'});
                return;
            }
            //全选按钮
            this.clickAll(this.element.siblings().find(".checkbox-state-all"));
            //单个选项点击事件
            this.clickOne(this.element.children('.checkbox-div'),this.element.siblings().find(".checkbox-state-all"));

        },
        //单选框联动
        clickOne:function(checkedOneDom,checkedAllDom){
            var _this = this;
            checkedOneDom.off('click').on('click',function() {
                if ($(this).hasClass("checked")) {
                    $(this).removeClass("checked").children('input').removeProp('checked');
                    _this.settings.checkedNum--;
                } else {
                    $(this).addClass("checked").children('input').prop('checked',true);
                    _this.settings.checkedNum++;
                }
                if (checkedAllDom) {
                    if (_this.settings.checkedNum == _this.element.find('.checkbox-div').length) {
                        checkedAllDom.children('.checkbox-all').addClass('checked');
                    } else {
                        checkedAllDom.children('.checkbox-all').removeClass('checked');
                    }
                }
            })
        },
        //全选框联动
        clickAll:function (dom) {
            if(this.settings.checkedNum == this.element.find('.checkbox-div').length){
                dom.children('.checkbox-all').addClass('checked');
            }else{
                dom.children('.checkbox-all').removeClass('checked');
            }
            var _this = this;
            dom.off('click').on('click',function(){
                if($(this).children('.checkbox-all').hasClass('checked')){
                    $(this).children('.checkbox-all').removeClass('checked');
                    _this.element.find('.checkbox-div').removeClass('checked');
                    _this.settings.checkedNum = 0;
                }else{
                    $(this).children('.checkbox-all').addClass('checked').children('.input-checkbox').removeProp('checked');
                    _this.element.find('.checkbox-div').addClass('checked').children('.input-checkbox').prop('checked',true);
                    _this.settings.checkedNum = _this.settings.rendersData.length;
                }
            });
        },
        /*	返回 true数据	*/
        backNameArr:function () {
            this.settings.backParaNameArr = [];
            for (var i = 0;i<this.element.find('.checkbox-div').length;i++) {
                if(this.element.find('.checkbox-div').eq(i).hasClass('checked')){
                    this.settings.backParaNameArr.push(this.element.find('.checkbox-div').eq(i).find('.checkbox-text').html());
                }
            }
            return this.settings.backParaNameArr;
        },
        backIdArr:function () {
            this.settings.backParaIdArr = [];
            for (var i = 0;i<this.element.find('.checkbox-div').length;i++) {
                if(this.element.find('.checkbox-div').eq(i).hasClass('checked')){
                    this.settings.backParaIdArr.push(this.element.find('.input-checkbox').eq(i).val());
                }
            }
            return this.settings.backParaIdArr;
        },
        /*  重置 */
        resetChecked:function () {
            this.settings.checkedNum = 0;
            this.settings.backParaNameArr = [];
            this.settings.backParaIdArr = [];
            this.element.find('.checkbox-div').removeClass('checked').children('input').removeProp('checked');
            //渲染回显数据
            if(this.settings.checkedIds.length>0){
                for(var i=0;i< this.settings.checkedIds.length;i++){
                    for(var j=0;j< this.element.find('.checkbox-div').length;j++) {
                        if (this.element.find('.checkbox-div').eq(j).children('input').val() == this.settings.checkedIds[i]) {
                            this.element.find('.checkbox-div').eq(j).addClass('checked').children('input').prop('checked', true);
                            this.settings.checkedNum++;
                        }
                    }
                }
            }
            //全选按钮
            this.clickAll(this.element.siblings().find(".checkbox-state-all"));
        },
        //清空实例
        emptyInstance:function () {
            this.element.find('.checkbox-div').removeClass('checked').children('input').removeProp('checked');
            this.element.data('plugin_' + pluginName,null)
        }
    };
// 对构造函数的一个轻量级封装，
// 防止产生多个实例
    $.fn[pluginName] = function ( settings ) {
        var args = arguments;
        if (settings === undefined || typeof settings === 'object') {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new CheckboxPlugin(this, settings));
                }
            });
        } else if (typeof settings === 'string' && settings[0] !== '_' && settings !== 'init') {
            var returns;
            this.each(function () {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof CheckboxPlugin && typeof instance[settings] === 'function') {
                    returns = instance[settings].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
            return returns !== undefined ? returns : this;
        }
    };

})( jQuery, window, document );

/*
* 滑块
* */
;(function ( $, window, document, undefined ) {
    // 声明默认属性对象
    var pluginName = "topNetSlider";
    var defaults = {
        stateOn:true
    };
    function SliderPlugin(element,settings) {
        this.element = $(element);
        this.settings = $.extend({},defaults,settings);
        this.init();
    }
    SliderPlugin.prototype = {
        init: function () {
            var initHtml = '';
            initHtml = '<div class="slider-div" style="float: left">' +
                '<input class="slider-input" type="checkbox" checked="'+ this.settings.checked +'"/>' +
                '<img class="slider-img" src=""/>' +
                '<span class="text-off">否</span>' +
                '<span class="text-on">是</span>' +
                '</div>';

            this.element.html(initHtml);
            //初始化
            this.resetSlider();

            var _this = this;
            this.element.children('.slider-div').off('click').on('click',function () {
                if($(this).hasClass('slider-blue')) {
                    $(this).removeClass('slider-blue').children('.slider-input').removeProp('checked').val("off").siblings('.slider-img').animate({left: "2px"}).siblings('.text-off').show(300).siblings('.text-on').hide(300);
                } else{
                    $(this).addClass('slider-blue').children('.slider-input').prop('checked',true).val("on").siblings('.slider-img').animate({left: "40px"}).siblings('.text-off').hide(300).siblings('.text-on').show(300);
                }
                _this.state = $(this).find('.slider-input').val();
            });
        },
        backParms:function () {
            return this.state;
        },
        resetSlider:function () {
            this.state = this.settings.checked==true?'on':'off';
            if(this.settings.checked == true) {
                this.element.children('.slider-div').addClass('slider-blue').find('.slider-input').prop('checked',true).val("on");
                this.element.children('.slider-div').find('.slider-img').animate({left: "40px"});
                this.element.children('.slider-div').find('.text-off').hide(300);
                this.element.children('.slider-div').find('.text-on').show(300);
            }else{
                this.element.children('.slider-div').removeClass('slider-blue').find('.slider-input').removeProp('checked').val("off");
                this.element.children('.slider-div').find('.slider-img').animate({left: "2px"});
                this.element.children('.slider-div').find('.text-off').show(300);
                this.element.children('.slider-div').find('.text-on').hide(300);
            }
        },
        //清空实例
        emptyInstance:function () {
            this.element.data('plugin_' + pluginName,null)
        }
    };
// 对构造函数的一个轻量级封装，
// 防止产生多个实例
    $.fn[pluginName] = function ( settings ) {
        var args = arguments;
        if (settings === undefined || typeof settings === 'object') {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new SliderPlugin(this, settings));
                }
            });
        }else if (typeof settings === 'string' && settings[0] !== '_' && settings !== 'init') {
            var returns;
            this.each(function () {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof SliderPlugin && typeof instance[settings] === 'function') {
                    returns = instance[settings].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
            return returns !== undefined ? returns : this;
        }
    };

})( jQuery, window, document );
/*
模拟select 下拉选择框
 */
;(function ( $, window, document, undefined ) {
    // 声明默认属性对象
    var pluginName = "topNetSelect";
    var defaults = {
        img:false
    };
    function SelectPlugin(element,settings) {
        this.$ele = $(element);
        this.settings = $.extend({},defaults,settings);
        this.init();
    }
    SelectPlugin.prototype = {
        //初始化
        init: function () {
            this.render();
            this.bindEvent();
        },
        //渲染dom
        render:function(){
            if (!this.$ele.children().length) {
                var p = '<p class="backPram">请选择</p>';
                var input = '<input type="hidden" name="" value="">'; //存值
                var ul = '<ul></ul>'
                this.$ele.append(p);
                this.$ele.append(input);
                this.$ele.children('input').prop('name',this.settings.inputName)
                this.$ele.append(ul);
                this.$ele.children('ul').css('display','none');
                this.$ele.attr("onselectstart","return false");
                if(this.settings.img){
                    for (var i = 0;i<this.settings.dataJson.length;i++){
                        var data = this.settings.dataJson[i];
                        var li = '<li data-id="' + data.key + '"><img src="'+data.value+'" ></li>'
                        this.$ele.children('ul').append(li);
                    }
                }
                else {
                    for (var i = 0;i<this.settings.dataJson.length;i++){
                        var data = this.settings.dataJson[i];
                        var li = '<li data-id="' + data.key + '">'+ data.value + '</li>'
                        this.$ele.children('ul').append(li);
                    }
                }
            }
            var _this = this;
            //渲染回显数据
            if(this.settings.selectedId){
                this.$ele.children('ul').children().each(function () {

                    if($(this).data('id') == _this.settings.selectedId) {
                        $(this).addClass('selected');
                        $(this).siblings('li').removeClass('selected');
                        _this.$ele.children('p').html($(this).html());
                        _this.$ele.children('input').val($(this).data('id'));
                        //记录选中数据
                        _this.backParaName = $(this).text();
                        _this.backParaId = $(this).data('id');
                    }
                });
            }
        },
        //事件
        bindEvent:function(){
            var _this = this;
            this.$ele.children('ul').children().off('click').on('click',function (e) {
                e.stopPropagation();
                $(this).addClass('selected').siblings().removeClass('selected');
                _this.$ele.children('p').html($(this).html());
                _this.$ele.children('input').val($(this).data('id'));
                _this.$ele.children('ul').css('display','none')
            });
            this.$ele.off('click').on('click',function () {
                _this.$ele.children('ul').toggle();
            });
            $(document).off('click').on('click',function (e) {
                var target = $(e.target);
                if (target.closest(".selectWrapper").length == 0) {
                    $(".selectWrapper").children('ul').css('display','none');
                }
            });
        },
        //返回选中信息
        backName:function () {
            return this.$ele.children('p').text();
        },
        backId:function () {
            return this.$ele.children('input').val();
        },
        //重置
        resetSelect:function () {
            this.$ele.children('p').text(!this.backParaName ? '请选择' : this.backParaName);
            this.$ele.children('input').val(!this.backParaId? '' : this.backParaId);
            //渲染回显数据
            var _this = this;
            if (this.settings.selectedId) {
                this.$ele.children('ul').children().each(function () {
                    if ($(this).data('id') == _this.settings.selectedId) {
                        $(this).addClass('selected').siblings().removeClass('selected');

                        _this.$ele.children('p').html($(this).html());
                        _this.$ele.children('input').val($(this).data('id'));
                        //记录选中数据
                        _this.backParaName = $(this).text();
                        _this.backParaId = $(this).data('id');
                    }
                });
            }
        },
        emptyInstance:function () {
					// this.ele.find('.select-tag').remove();
					// 	this.$ele.find('li').removeClass('selected');
            this.$ele.data('plugin_' + pluginName,null)
        },
				//重置为空
				resetSelectInEmpty:function () {
					this.$ele.children('p').text('请选择');
					this.$ele.children('input').val('');
				}
    };
// 对构造函数的一个轻量级封装，
// 防止产生多个实例
    $.fn[pluginName] = function ( settings ) {
        var args = arguments;

        if (settings === undefined || typeof settings === 'object') {
            return this.each(function () {

                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new SelectPlugin(this, settings));
                }
            });
        }else if (typeof settings === 'string' && settings[0] !== '_' && settings !== 'init') {
            var returns;
            this.each(function () {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof SelectPlugin && typeof instance[settings] === 'function') {
                    returns = instance[settings].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
            return returns !== undefined ? returns : this;
        }
    };

})( jQuery, window, document );
